#!/bin/bash

set -e

CMAKE="/home/huxiao/code/cmake-3.31.3-linux-x86_64/bin/cmake"
PROJECT_DIR=$(pwd)
BUILD_DIR="$PROJECT_DIR/build-ohos"
INSTALL_DIR="$BUILD_DIR/output"
BUILD_LOG="$BUILD_DIR/build.log"

# === 设置 OHOS 工具链环境 ===
setarm64ENV() {
    export AS=${OHOS_SDK}/native/llvm/bin/llvm-as
    export CC=${OHOS_SDK}/native/llvm/bin/aarch64-unknown-linux-ohos-clang
    export CXX=${OHOS_SDK}/native/llvm/bin/aarch64-unknown-linux-ohos-clang++
    export LD=${OHOS_SDK}/native/llvm/bin/ld.lld
    export STRIP=${OHOS_SDK}/native/llvm/bin/llvm-strip
    export RANLIB=${OHOS_SDK}/native/llvm/bin/llvm-ranlib
    export OBJDUMP=${OHOS_SDK}/native/llvm/bin/llvm-objdump
    export OBJCOPY=${OHOS_SDK}/native/llvm/bin/llvm-objcopy
    export NM=${OHOS_SDK}/native/llvm/bin/llvm-nm
    export AR=${OHOS_SDK}/native/llvm/bin/llvm-ar

    export CFLAGS="-DOHOS_NDK -fPIC -D__MUSL__=1 -Wno-error=sign-compare -Wno-ignored-qualifiers -Wno-error=unused-parameter -Wno-error=#warnings"
    export CXXFLAGS=${CFLAGS}
    export LDFLAGS="-lhilog_ndk.z -lohaudio -lohcamera -limage_receiver -lnative_image -lohimage \
        -lnative_buffer -lnative_media_codecbase \
        -lnative_media_core -lnative_media_vdec -lc++_shared \
        -lnative_window -lEGL -lGLESv3 -lnative_media_venc \
        -L/home/huxiao/code/linphone-sdk/build-ohos/output/lib/libyuv.so"

    # -L/home/huxiao/code/tpc_c_cplusplus/lycium/usr/alsa-lib/arm64-v8a/lib
    # export CMAKE_PREFIX_PATH=/home/huxiao/code/tpc_c_cplusplus/lycium/usr/alsa-lib/arm64-v8a:$CMAKE_PREFIX_PATH
}

# === 清理构建目录 ===
clean_project() {
    if [ -d "$BUILD_DIR" ]; then
        echo "Running cmake clean target..."
        ${CMAKE} --build "$BUILD_DIR" --target clean || true
        echo "Removing build directory for full cleanup..."
        rm -rf "$BUILD_DIR"
    else
        echo "Nothing to clean."
    fi
    echo "Clean complete."
}

# === 构建流程 ===
build_project() {
    setarm64ENV

    # rm -rf "$BUILD_DIR"
    echo "Starting build..."
    mkdir -p "$BUILD_DIR"
    cd "$BUILD_DIR"

    ${CMAKE} .. \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_SYSTEM_NAME=Linux \
        -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
        -DCMAKE_CROSSCOMPILING=TRUE \
        -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
        -DENABLE_GPL_THIRD_PARTIES=ON \
        -DENABLE_V4L=0 \
        -DENABLE_DAV1D=OFF \
        -DENABLE_SOUND=YES \
        -DENABLE_VIDEO=YES \
        -DENABLE_NON_FREE_FEATURES=ON \
        -DENABLE_OPENH264=OFF \
        -DBUILD_AOM=OFF \
        -DBUILD_DAV1D=OFF \
        -DENABLE_UNIT_TESTS=OFF \
        -DENABLE_SCREENSHARING=NO \
        -DENABLE_DEBUG_LOGS=YES \
        -DENABLE_AV1=NO \
        -DBUILD_LIBVPX=NO \
        -DCMAKE_EXE_LINKER_FLAGS="$LDFLAGS" > "$BUILD_LOG" 2>&1

    if [ $? -ne 0 ]; then
        echo "Error: CMAKE failed. Check build.log"
        exit 1
    fi

    ${CMAKE} --build . --target install --parallel $(nproc) -- VERBOSE=1 >> "$BUILD_LOG" 2>&1

    if [ $? -ne 0 ]; then
        echo "Error: Build failed. Check build.log"
        exit 1
    fi

    echo "Build successful. Output at: $INSTALL_DIR"
}

# === 主入口 ===
case "$1" in
    -c|--clean)
        clean_project
        ;;
    *)
        build_project
        ;;
esac
